<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line1 flex-s">
          <el-button type="primary" @click="onOperation({}, 'add')"
            ><el-icon style="margin-right: 2px"><Plus /></el-icon
            >新建资讯</el-button
          >
        </div>
        <div class="header-line2 flex-s">
          <el-input
            v-model="params.biaoti"
            placeholder="请输入标题"
            style="width: 350px"
            size="large"
          >
            <template #append>
              <el-button
                @click="GetList"
                style="min-width: 50px"
                icon="Search"
              />
            </template>
          </el-input>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="params.leixing"
            size="large"
            placeholder="类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in type"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
          <el-col :span="8">
            <el-date-picker
              v-model="chooseTime"
              type="datetimerange"
              range-separator="To"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              size="large"
              class="ml20"
              prefix-icon="Calendar"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="changeTime"
            />
          </el-col>
        </div>
      </div>
      <div class="content content-table">
        <el-table
          ref="multipleTableRef"
          border
          :data="list"
          style="width: calc(100% - 20px)"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            property="id"
            label="序号"
            width="100"
            align="center"
          />
          <el-table-column property="biaoti" label="标题" align="center" />
          <el-table-column
            property="leixing"
            label="类型"
            width="130"
            align="center"
          />
          <el-table-column
            property="shangjiaTime"
            label="发布时间"
            width="300"
            align="center"
          />
          <el-table-column label="状态" align="center">
            <template #default="scope">
              <span>{{
                scope.row.shangjia.code == 0 ? "已下架" : "已上架"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            property="liulanliang"
            label="浏览量"
            width="200"
            align="center"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="250"
            align="center"
          >
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'detail')"
                >详情</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="changeStatus(scope.row)"
                >{{ scope.row.shangjia.code == 0 ? "上架" : "下架" }}</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click="onOperation(scope.row, 'delete')"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <h-page
          v-if="total"
          v-model:curr-page="params.current"
          :size="params.size"
          :total="total"
          @change="GetList"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { defineComponent, toRefs, reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  GetDynamicList,
  DeleteDynamic,
  ChangeDynamicStatus,
} from "@/api/dynamic";
import { baseMsg, elMessageTip } from "@/utils/utils";
import { GetDictionaryTree } from "@/api/system";
export default defineComponent({
  setup() {
    const route = useRoute();
    const router = useRouter();
    const state = reactive({
      params: {
        current: Number(route.query.page) || 1,
        size: Number(route.query.size) || 10,
        biaoti: "",
        leixing: "",
        kaishishijian: "",
        jieshushijian: "",
      },
      list: [],
      type: [],
      // 时间选择器时间
      chooseTime: [],
      total: 0,
    });
    // 资讯动态类型下拉查询
    const GetType = async () => {
      const res = await GetDictionaryTree({ dicModel: "dongtaizixunleixing" });
      if (res.code == 4201) {
        state.type = res.data;
      }
    };
    const GetList = async () => {
      const res = await GetDynamicList(state.params);
      state.list = res.data.records;
      state.total = res.data.total;
    };
    // 时间选择
    const changeTime = (value) => {
      if (value) {
        state.params.kaishishijian = value[0];
        state.params.jieshushijian = value[1];
      } else {
        state.params.kaishishijian = '';
        state.params.jieshushijian = '';
      }
      GetList();
    };
    // 操作
    const onOperation = (params, operation) => {
      if (operation === "detail") {
        router.push({
          path: `/zixundongtai/detail`,
          query: {
            id: params.id,
          },
        });
      }
      if (operation === "add" || operation === "edit") {
        router.push({
          path: `/zixundongtai/info`,
          query: {
            id: params.id || null,
            type: operation,
          },
        });
      }
      if (operation === "delete") {
        elMessageTip({
          ok() {
            DeleteDynamic({
              id: params.id,
            }).then((res) => {
              if (res.code == 4201 || res.code == 4202) {
                baseMsg("删除成功");
                GetList();
              }
            });
          },
        });
      }
    };
    // 上下架
    const changeStatus = async (value) => {
      const res = await ChangeDynamicStatus({ id: value.id });
      if (res.code === 4202) {
        baseMsg("操作成功");
        GetList();
      }
    };
    watch(
      () => route.query,
      () => {
        state.params.current = Number(route.query.page) || 1;
        GetList();
      },
      { deep: true }
    );
    GetList();
    GetType();
    return {
      ...toRefs(state),
      GetList,
      onOperation,
      changeTime,
      changeStatus,
      GetType,
    };
  },
});
</script>
<style lang="scss" scoped>
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
}
</style>
<style lang="scss">
.content-table .el-table__header th {
  background-color: #f2f9fc !important;
}
</style>
